package com.cyt.work.enrollment.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.BaseUtil;
import com.cyt.base.util.DictCode;
import com.cyt.base.util.ErrorCode;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.service.SysFileService;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.cyt.sys.util.EasyPoiUtil;
import com.cyt.sys.util.FilePathUtil;
import com.cyt.sys.util.GlobalExcelDictHandler;
import com.cyt.work.enrollment.dao.EnrollmentHomeinfoDao;
import com.cyt.work.enrollment.dao.EnrollmentReleaseDao;
import com.cyt.work.enrollment.dao.EnrollmentStuinfoDao;
import com.cyt.work.enrollment.dao.EnrollmentTestDao;
import com.cyt.work.enrollment.model.po.*;
import com.cyt.work.enrollment.model.vo.*;
import com.cyt.work.enrollment.service.EnrollmentHomeinfoService;
import com.cyt.work.enrollment.service.EnrollmentReleaseService;
import com.cyt.work.enrollment.service.EnrollmentStuinfoService;
import com.cyt.work.enrollment.service.EnrollmentTestService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.*;

/**
 * 自主招生-招生学生信息Service层实现类
 *
 * @author caideyong
 * @date 2019-11-22 16:30:39
 */
@Service("enrollmentStuinfoService")
public class EnrollmentStuinfoServiceImpl extends BaseServiceImpl<EnrollmentStuinfo> implements EnrollmentStuinfoService {

    @Resource
    private EnrollmentStuinfoDao enrollmentStuinfoDao;
    @Autowired
    private EnrollmentHomeinfoService enrollmentHomeinfoService;
    @Autowired
    private EnrollmentTestService enrollmentTestService;
    @Autowired
    private EnrollmentHomeinfoDao enrollmentHomeinfoDao;
    @Autowired
    private EnrollmentTestDao enrollmentTestDao;
    @Autowired
    private SysFileService sysFileService;
    @Autowired
    private EnrollmentReleaseDao enrollmentReleaseDao;
    @Autowired
    private EnrollmentReleaseService enrollmentReleaseService;

    @Autowired
    public void setBaseDao(EnrollmentStuinfoDao enrollmentStuinfoDao) {
        this.baseDao = enrollmentStuinfoDao;
    }

    /**
     * 查询对象集合
     *
     * @param enrollmentStuinfoVo 对象
     * @return List 对象集合
     * @author caideyong
     * @date 2019-11-22 16:30:39
     */
    @Override
    public List<EnrollmentStuinfo> listExample(EnrollmentStuinfoVo enrollmentStuinfoVo) {
        return enrollmentStuinfoDao.getEnrollmetStuinfoList(enrollmentStuinfoVo);
    }

    /**
     * 分页查询
     *
     * @param enrollmentStuinfoVo 接受参数对象
     * @param pageNum             分页数
     * @param pageSize            每页显示条数
     * @return ReturnResult
     * @author caideyong
     * @date 2019-11-22 16:30:39
     */
    @Override
    public ReturnResult page(EnrollmentStuinfoVo enrollmentStuinfoVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<EnrollmentStuinfo> list = listExample(enrollmentStuinfoVo);
        PageInfo<EnrollmentStuinfo> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 添加或更新
     *
     * @param enrollmentStuinfoVo 参数接受对象
     * @param sysUser             当前用户
     * @return ReturnResult
     * @author caideyong
     * @date 2019-11-22 16:30:39
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(EnrollmentStuinfoVo enrollmentStuinfoVo, SysUser sysUser) {
        //查询主键id是否存在,如果存在就更新,不存在就新建
        if (StringUtils.isBlank(enrollmentStuinfoVo.getId())) {
            EnrollmentStuinfo enrollmentStuinfo = new EnrollmentStuinfo(sysUser);
            enrollmentStuinfo.setId(BaseUtil.uId());
            enrollmentStuinfo.setName(enrollmentStuinfoVo.getName());
            enrollmentStuinfo.setSex(enrollmentStuinfoVo.getSex());
            enrollmentStuinfo.setPoliticalStatus(enrollmentStuinfoVo.getPoliticalStatus());
            enrollmentStuinfo.setNationality(enrollmentStuinfoVo.getNationality());
            enrollmentStuinfo.setAccountType(enrollmentStuinfoVo.getAccountType());
            enrollmentStuinfo.setPhone(enrollmentStuinfoVo.getPhone());
            enrollmentStuinfo.setCard(enrollmentStuinfoVo.getCard());
            enrollmentStuinfo.setSchool(enrollmentStuinfoVo.getSchool());
            enrollmentStuinfo.setSchoolPlace(enrollmentStuinfoVo.getSchoolPlace());
            enrollmentStuinfo.setAccountPlace(enrollmentStuinfoVo.getAccountPlace());
            enrollmentStuinfo.setTestNum(enrollmentStuinfoVo.getTestNum());
            enrollmentStuinfo.setPostNum(enrollmentStuinfoVo.getPostNum());
            enrollmentStuinfo.setGradeNum(enrollmentStuinfoVo.getGradeNum());
            enrollmentStuinfo.setClassNum(enrollmentStuinfoVo.getClassNum());
            enrollmentStuinfo.setPlace(enrollmentStuinfoVo.getPlace());
            enrollmentStuinfo.setReward(enrollmentStuinfoVo.getReward());
            enrollmentStuinfo.setReviewState("1");//新建,待审核
            enrollmentStuinfo.setReleaseId(enrollmentStuinfoVo.getReleaseId());
            enrollmentStuinfo.setPic(enrollmentStuinfoVo.getPic());
            enrollmentStuinfoDao.insert(enrollmentStuinfo);
            enrollmentStuinfoVo.setId(enrollmentStuinfo.getId());
            enrollmentStuinfoVo.setStatus("1");//新建
            if (StringUtils.isNotBlank(enrollmentStuinfoVo.getHomeInfo())) {
                saveHomeInfo(enrollmentStuinfoVo);
            }
            if (StringUtils.isNotBlank(enrollmentStuinfoVo.getTestInfo())) {
                saveTestInfo(enrollmentStuinfoVo);
            }
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
        } else {
            EnrollmentStuinfo enrollmentStuinfo = enrollmentStuinfoDao.selectByPrimaryKey(enrollmentStuinfoVo.getId());
            enrollmentStuinfo.setName(enrollmentStuinfoVo.getName());
            enrollmentStuinfo.setSex(enrollmentStuinfoVo.getSex());
            enrollmentStuinfo.setPoliticalStatus(enrollmentStuinfoVo.getPoliticalStatus());
            enrollmentStuinfo.setNationality(enrollmentStuinfoVo.getNationality());
            enrollmentStuinfo.setAccountType(enrollmentStuinfoVo.getAccountType());
            enrollmentStuinfo.setPhone(enrollmentStuinfoVo.getPhone());
            enrollmentStuinfo.setCard(enrollmentStuinfoVo.getCard());
            enrollmentStuinfo.setSchool(enrollmentStuinfoVo.getSchool());
            enrollmentStuinfo.setSchoolPlace(enrollmentStuinfoVo.getSchoolPlace());
            enrollmentStuinfo.setAccountPlace(enrollmentStuinfoVo.getAccountPlace());
            enrollmentStuinfo.setTestNum(enrollmentStuinfoVo.getTestNum());
            enrollmentStuinfo.setPostNum(enrollmentStuinfoVo.getPostNum());
            enrollmentStuinfo.setGradeNum(enrollmentStuinfoVo.getGradeNum());
            enrollmentStuinfo.setClassNum(enrollmentStuinfoVo.getClassNum());
            enrollmentStuinfo.setPlace(enrollmentStuinfoVo.getPlace());
            enrollmentStuinfo.setReward(enrollmentStuinfoVo.getReward());
            if (StringUtils.isNotBlank(enrollmentStuinfoVo.getReviewState())) {
                enrollmentStuinfo.setReviewState(enrollmentStuinfoVo.getReviewState());
                enrollmentStuinfo.setReviewCmmt(enrollmentStuinfoVo.getReviewCmmt());
            }
            enrollmentStuinfo.setReleaseId(enrollmentStuinfoVo.getReleaseId());
            enrollmentStuinfo.setPic(enrollmentStuinfoVo.getPic());
            enrollmentStuinfoDao.updateByPrimaryKey(enrollmentStuinfo);
            enrollmentStuinfoVo.setStatus("2");//编辑
            if (StringUtils.isNotBlank(enrollmentStuinfoVo.getHomeInfo())) {
                saveHomeInfo(enrollmentStuinfoVo);
            }
            if (StringUtils.isNotBlank(enrollmentStuinfoVo.getTestInfo())) {
                saveTestInfo(enrollmentStuinfoVo);
            }
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdatePhone(EnrollmentStuinfoVo enrollmentStuinfoVo) {
        //查询主键id是否存在,如果存在就更新,不存在就新建
        if (StringUtils.isBlank(enrollmentStuinfoVo.getId())) {
            EnrollmentStuinfo enrollmentStuinfo = new EnrollmentStuinfo();
            enrollmentStuinfo.setId(BaseUtil.uId());
            enrollmentStuinfo.setName(enrollmentStuinfoVo.getName());
            enrollmentStuinfo.setSex(enrollmentStuinfoVo.getSex());
            enrollmentStuinfo.setPoliticalStatus(enrollmentStuinfoVo.getPoliticalStatus());
            enrollmentStuinfo.setNationality(enrollmentStuinfoVo.getNationality());
            enrollmentStuinfo.setAccountType(enrollmentStuinfoVo.getAccountType());
            enrollmentStuinfo.setPhone(enrollmentStuinfoVo.getPhone());
            enrollmentStuinfo.setCard(enrollmentStuinfoVo.getCard());
            enrollmentStuinfo.setSchool(enrollmentStuinfoVo.getSchool());
            enrollmentStuinfo.setSchoolPlace(enrollmentStuinfoVo.getSchoolPlace());
            enrollmentStuinfo.setAccountPlace(enrollmentStuinfoVo.getAccountPlace());
            enrollmentStuinfo.setTestNum(enrollmentStuinfoVo.getTestNum());
            enrollmentStuinfo.setPostNum(enrollmentStuinfoVo.getPostNum());
            enrollmentStuinfo.setGradeNum(enrollmentStuinfoVo.getGradeNum());
            enrollmentStuinfo.setClassNum(enrollmentStuinfoVo.getClassNum());
            enrollmentStuinfo.setPlace(enrollmentStuinfoVo.getPlace());
            enrollmentStuinfo.setReward(enrollmentStuinfoVo.getReward());
            enrollmentStuinfo.setReviewState("1");//新建,待审核
            enrollmentStuinfo.setReleaseId(enrollmentStuinfoVo.getReleaseId());
            enrollmentStuinfo.setPic(enrollmentStuinfoVo.getPic());
            enrollmentStuinfo.setCreateTime(new Date());
            enrollmentStuinfo.setRemoveTag(BaseConstant.REMOVE_TAG_NO);
            enrollmentStuinfoDao.insert(enrollmentStuinfo);
            enrollmentStuinfoVo.setId(enrollmentStuinfo.getId());
            enrollmentStuinfoVo.setStatus("1");//新建
            if (StringUtils.isNotBlank(enrollmentStuinfoVo.getHomeInfo())) {
                saveHomeInfo(enrollmentStuinfoVo);
            }
            if (StringUtils.isNotBlank(enrollmentStuinfoVo.getTestInfo())) {
                saveTestInfo(enrollmentStuinfoVo);
            }
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
        } else {
            EnrollmentStuinfo enrollmentStuinfo = enrollmentStuinfoDao.selectByPrimaryKey(enrollmentStuinfoVo.getId());
            enrollmentStuinfo.setName(enrollmentStuinfoVo.getName());
            enrollmentStuinfo.setSex(enrollmentStuinfoVo.getSex());
            enrollmentStuinfo.setPoliticalStatus(enrollmentStuinfoVo.getPoliticalStatus());
            enrollmentStuinfo.setNationality(enrollmentStuinfoVo.getNationality());
            enrollmentStuinfo.setAccountType(enrollmentStuinfoVo.getAccountType());
            enrollmentStuinfo.setPhone(enrollmentStuinfoVo.getPhone());
            enrollmentStuinfo.setCard(enrollmentStuinfoVo.getCard());
            enrollmentStuinfo.setSchool(enrollmentStuinfoVo.getSchool());
            enrollmentStuinfo.setSchoolPlace(enrollmentStuinfoVo.getSchoolPlace());
            enrollmentStuinfo.setAccountPlace(enrollmentStuinfoVo.getAccountPlace());
            enrollmentStuinfo.setTestNum(enrollmentStuinfoVo.getTestNum());
            enrollmentStuinfo.setPostNum(enrollmentStuinfoVo.getPostNum());
            enrollmentStuinfo.setGradeNum(enrollmentStuinfoVo.getGradeNum());
            enrollmentStuinfo.setClassNum(enrollmentStuinfoVo.getClassNum());
            enrollmentStuinfo.setPlace(enrollmentStuinfoVo.getPlace());
            enrollmentStuinfo.setReward(enrollmentStuinfoVo.getReward());
            if (StringUtils.isNotBlank(enrollmentStuinfoVo.getReviewState())) {
                enrollmentStuinfo.setReviewState(enrollmentStuinfoVo.getReviewState());
                enrollmentStuinfo.setReviewCmmt(enrollmentStuinfoVo.getReviewCmmt());
            }
            enrollmentStuinfo.setReleaseId(enrollmentStuinfoVo.getReleaseId());
            enrollmentStuinfo.setPic(enrollmentStuinfoVo.getPic());
            enrollmentStuinfo.setUpdateTime(new Date());
            enrollmentStuinfoDao.updateByPrimaryKey(enrollmentStuinfo);
            enrollmentStuinfoVo.setStatus("2");//编辑
            if (StringUtils.isNotBlank(enrollmentStuinfoVo.getHomeInfo())) {
                saveHomeInfo(enrollmentStuinfoVo);
            }
            if (StringUtils.isNotBlank(enrollmentStuinfoVo.getTestInfo())) {
                saveTestInfo(enrollmentStuinfoVo);
            }
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
        }
    }

    /**
     * 审核
     *
     * @param enrollmentStuinfoVo 参数接受对象
     * @param sysUser             当前用户
     * @return ReturnResult
     * @author caideyong
     * @date 2019-11-22 16:30:39
     */
    @Override
    public ReturnResult check(EnrollmentStuinfoVo enrollmentStuinfoVo, SysUser sysUser) {
        if (StringUtils.isNotBlank(enrollmentStuinfoVo.getId())) {
            EnrollmentStuinfo enrollmentStuinfo = enrollmentStuinfoDao.selectByPrimaryKey(enrollmentStuinfoVo);
            EnrollmentRelease enrollmentRelease = enrollmentReleaseDao.selectByPrimaryKey(enrollmentStuinfoVo.getReleaseId());
            EnrollmentStuinfoVo enrollmentStuinfoVo1 = new EnrollmentStuinfoVo();
            enrollmentStuinfoVo1.setReleaseId(enrollmentStuinfoVo.getReleaseId());
            enrollmentStuinfoVo1.setReviewState("2");//已通过
            List<EnrollmentStuinfo> enrollmentStuinfoList = enrollmentStuinfoDao.getEnrollmetStuinfoList(enrollmentStuinfoVo1);//已通过的学生信息
            if (enrollmentStuinfoList.size() < Integer.parseInt(enrollmentRelease.getStuNum())) {
                enrollmentStuinfo.setReviewState(enrollmentStuinfoVo.getReviewState());
                enrollmentStuinfo.setReviewCmmt(enrollmentStuinfoVo.getReviewCmmt());
                enrollmentStuinfo.setUpdator(sysUser.getId());
                enrollmentStuinfo.setUpdateTime(new Date());
                enrollmentStuinfoDao.updateByPrimaryKey(enrollmentStuinfo);
                if (StringUtils.isNotBlank(enrollmentStuinfo.getReviewState()) && enrollmentStuinfoVo.getReviewState().equals("2")) {
                    //再次查询已通过的人数
                    List<EnrollmentStuinfo> enrollmentStuinfoList1 = enrollmentStuinfoDao.getEnrollmetStuinfoList(enrollmentStuinfoVo1);//已通过的学生信息
                    if (enrollmentStuinfoList1.size() == Integer.parseInt(enrollmentRelease.getStuNum())) {
                        //结束此次招新，并把未审核的学生信息全部改成已驳回；
                        EnrollmentReleaseVo enrollmentReleaseVo = new EnrollmentReleaseVo();
                        enrollmentReleaseVo.setId(enrollmentStuinfoVo.getReleaseId());
                        enrollmentReleaseService.endRelease(enrollmentReleaseVo, sysUser);//结束招新
                        EnrollmentStuinfoVo enrollmentStuinfoVo2 = new EnrollmentStuinfoVo();
                        enrollmentStuinfoVo2.setReleaseId(enrollmentStuinfoVo.getReleaseId());
                        enrollmentStuinfoVo2.setReviewCmmt("招生人数已满！");
                        updateUnchecked(enrollmentStuinfoVo2);
                    }
                }
                return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
            } else {
                return new ReturnResult(ErrorCode.CODE_1, "招收人数已满，此次自主招生已结束");
            }
        } else {
            return new ReturnResult(ErrorCode.CODE_1, "该信息不存在");
        }
    }


    /**
     * 一键审核
     *
     * @param enrollmentStuinfoVo 参数接受对象
     * @param sysUser             当前用户
     * @return ReturnResult
     * @author caideyong
     * @date 2019-11-22 16:30:39
     */
    @Override
    public ReturnResult checkAll(EnrollmentStuinfoVo enrollmentStuinfoVo, SysUser sysUser) {
        if (StringUtils.isNotBlank(enrollmentStuinfoVo.getReleaseId())) {
            //一键审核未审核过得内容
            List<EnrollmentStuinfo> enrollmentStuinfoList = enrollmentStuinfoDao.getEnrollmetStuinfoListUncheck(enrollmentStuinfoVo);
            //速度特别慢？
            if (enrollmentStuinfoList.size() > 0) {
                //判断是同意还是驳回
                if (StringUtils.isNotBlank(enrollmentStuinfoVo.getReviewState()) && enrollmentStuinfoVo.getReviewState().equals("2")) {
                    //查询已同意的学生数量和此次招生的拟招生数量
                    EnrollmentStuinfoVo enrollmentStuinfoVo1 = new EnrollmentStuinfoVo();
                    enrollmentStuinfoVo1.setReleaseId(enrollmentStuinfoVo.getReleaseId());
                    enrollmentStuinfoVo1.setReviewState("2");
                    List<EnrollmentStuinfo> enrollmentStuinfoList1 = enrollmentStuinfoDao.getEnrollmetStuinfoList(enrollmentStuinfoVo1);
                    EnrollmentRelease enrollmentRelease = enrollmentReleaseDao.selectByPrimaryKey(enrollmentStuinfoVo.getReleaseId());
                    if (enrollmentStuinfoList1.size() < Integer.parseInt(enrollmentRelease.getStuNum())) {
                        //已报名人数小于拟报名人数
                        Integer num = Integer.parseInt(enrollmentRelease.getStuNum()) - enrollmentStuinfoList1.size();
                        for (int i = 0; i < num; i++) {
                            enrollmentStuinfoList.get(i).setReviewState(enrollmentStuinfoVo.getReviewState());
                            enrollmentStuinfoList.get(i).setReviewCmmt(enrollmentStuinfoVo.getReviewCmmt());
                            enrollmentStuinfoList.get(i).setUpdator(sysUser.getId());
                            enrollmentStuinfoList.get(i).setUpdateTime(new Date());
                            enrollmentStuinfoDao.updateByPrimaryKey(enrollmentStuinfoList.get(i));
                        }
                        EnrollmentStuinfoVo enrollmentStuinfoVo2=new EnrollmentStuinfoVo();
                        enrollmentStuinfoVo2.setReviewCmmt("招生人数已满！");
                        enrollmentStuinfoVo2.setReleaseId(enrollmentStuinfoVo.getReleaseId());
                        updateUnchecked(enrollmentStuinfoVo2);
                        EnrollmentReleaseVo enrollmentReleaseVo=new EnrollmentReleaseVo();
                        enrollmentReleaseVo.setId(enrollmentStuinfoVo.getReleaseId());
                        enrollmentReleaseService.endRelease(enrollmentReleaseVo,sysUser);
                    }
                    return new ReturnResult(ErrorCode.CODE_0,"招生人数已满,未审核的报名信息已自动驳回！");
                } else {
                    //未审批的全部改为已驳回
                    updateUnchecked(enrollmentStuinfoVo);
                    return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
                }
            } else {
                return new ReturnResult(ErrorCode.CODE_1, "没有待审核的信息");
            }
        } else {
            return new ReturnResult(ErrorCode.CODE_1, "此次招生发布信息不存在");
        }
    }

    /**
     * 根据条件逻辑删除
     *
     * @param enrollmentStuinfoVo 参数接受对象
     * @param sysUser             当前用户
     * @return ReturnResult
     * @author caideyong
     * @date 2019-11-22 16:30:39
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(EnrollmentStuinfoVo enrollmentStuinfoVo, SysUser sysUser) {
        //要修改的字段
        EnrollmentStuinfo enrollmentStuinfo = new EnrollmentStuinfo();
        enrollmentStuinfo.setRemoveTag(DictCode.BASE_REMOVE_TAG_DELETE);
        enrollmentStuinfo.setUpdator(sysUser.getId());
        enrollmentStuinfo.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(EnrollmentStuinfo.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(enrollmentStuinfoVo.getId())) {
            criteria.andIn("id", Arrays.asList(enrollmentStuinfoVo.getId().split(",")));
        }
        if (StringUtils.isNotBlank(enrollmentStuinfo.getReleaseId())) {
            criteria.andEqualTo("releaseId", enrollmentStuinfo.getReleaseId());
        }
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        enrollmentStuinfoDao.updateByExampleSelective(enrollmentStuinfo, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 根据条件物理删除
     *
     * @param enrollmentStuinfoVo 参数接受对象
     * @param sysUser             当前用户
     * @return ReturnResult
     * @author caideyong
     * @date 2019-11-22 16:30:39
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult delete(EnrollmentStuinfoVo enrollmentStuinfoVo, SysUser sysUser) {
        //筛选条件
        Example example = new Example(EnrollmentStuinfo.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(enrollmentStuinfoVo.getId())) {
            criteria.andIn("id", Arrays.asList(enrollmentStuinfoVo.getId().split(",")));
        }
        enrollmentStuinfoDao.deleteByExample(example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 导入学生报名信息
     *
     * @return ReturnResult
     * @author caideyong
     * @date 2019/12/5
     */
    @Override
    public ReturnResult importData(HttpServletRequest request, MultipartFile file, String catalog, String update, String applyId, SysUser sysUser, String releaseId) {
        // 1、不更新已存在数据，2、更新存在数据
        ReturnResult returnResult = sysFileService.fileUpload(file, request, catalog, BaseConstant.FILE_TYPE_EXECL);
        if (ErrorCode.CODE_0 == returnResult.getCode()) {
            String filePath = FilePathUtil.acceptCheckRealPath(request) + returnResult.getData().toString();
            ImportParams params = new ImportParams();
            params.setTitleRows(0);
            params.setHeadRows(1);
            params.setNeedVerify(true);
            // 解决多读取问题，如果出现没有导入问题，需要将该属性去除
            params.setKeyIndex(0);
            // 如果有code码需要设置，现有bug，需要修改源码CellValueService
            params.setDictHandler(new GlobalExcelDictHandler());
            ExcelImportResult<EnrollmentStuinfoVerifyImportVo> result = ExcelImportUtil.importExcelMore(new File(filePath), EnrollmentStuinfoVerifyImportVo.class, params);
            List<EnrollmentStuinfoVerifyImportVo> educationUserImportList = result.getList();
            if (CollUtil.isNotEmpty(educationUserImportList)) {
                //先删除已有的数据
                EnrollmentStuinfoVo e = new EnrollmentStuinfoVo();
                e.setReleaseId(releaseId);
                this.remove(e, sysUser);

                Map<String, EnrollmentStuinfoImportVo> educationUserMap = new HashMap<>();
                String key;
                for (EnrollmentStuinfoImportVo educationUser : educationUserImportList) {
                    key = educationUser.getCard();
                    if (educationUserMap.keySet().contains(key)) {
                        continue;
                    }
                    educationUserMap.put(key, educationUser);
                }
                List<EnrollmentStuinfo> enrollmentStuinfoList = new ArrayList<>();
                for (Map.Entry<String, EnrollmentStuinfoImportVo> entry : educationUserMap.entrySet()) {
                    // 名称不存在
                    EnrollmentStuinfoImportVo userFrom = entry.getValue();
                    EnrollmentStuinfo enrollmentStuinfo = new EnrollmentStuinfo();
                    enrollmentStuinfo.setId(BaseUtil.uId());
                    enrollmentStuinfo.setAccountPlace(userFrom.getAccountPlace());
                    enrollmentStuinfo.setAccountType(userFrom.getAccountType());
                    enrollmentStuinfo.setCard(userFrom.getCard());
                    enrollmentStuinfo.setClassNum(userFrom.getClassNum());
                    enrollmentStuinfo.setGradeNum(userFrom.getGradeNum());
                    enrollmentStuinfo.setName(userFrom.getName());
                    enrollmentStuinfo.setNationality(userFrom.getNationality());
                    enrollmentStuinfo.setPhone(userFrom.getPhone());
                    enrollmentStuinfo.setPlace(userFrom.getPlace());
                    enrollmentStuinfo.setPoliticalStatus(userFrom.getPoliticalStatus());
                    enrollmentStuinfo.setPostNum(userFrom.getPostNum());
                    enrollmentStuinfo.setReleaseId(releaseId);
                    enrollmentStuinfo.setSchool(userFrom.getSchool());
                    enrollmentStuinfo.setSchoolPlace(userFrom.getSchoolPlace());
                    enrollmentStuinfo.setSex(userFrom.getSex());
                    enrollmentStuinfo.setTestNum(userFrom.getTestNum());
                    enrollmentStuinfo.setReward(userFrom.getReward());
                    enrollmentStuinfo.setReviewState("1");
                    enrollmentStuinfo.setRemoveTag(BaseConstant.REMOVE_TAG_NO);
                    enrollmentStuinfo.setCreator(sysUser.getId());
                    enrollmentStuinfo.setCreateTime(new Date());
                    enrollmentStuinfoList.add(enrollmentStuinfo);
                }
                // 保存
                if (enrollmentStuinfoList != null && enrollmentStuinfoList.size() > 0) {
                    this.insertList(enrollmentStuinfoList);
                }
            } else {
                // 行号从0开始，要加上titleRows和headRows
                if (result.isVerfiyFail()) {
                    // 如果错误，打印出提醒，直接返回
                    int successNum = 0;
                    int failNum = result.getFailList().size();
                    if (result.getList() != null && result.getList().size() > 0) {
                        successNum = result.getList().size();
                    }
                    String errorMsg = String.format("共导入%s条，成功%s条，失败%s条。", successNum + failNum, successNum, failNum);
                    List<EnrollmentStuinfoVerifyImportVo> failList = result.getFailList();
                    return new ReturnResult(ErrorCode.CODE_1, failList, errorMsg);
                }
            }
        }
        return returnResult; // 1、不更新已存在数据，2、更新存在数据
    }

    /**
     * 批量保存
     *
     * @author caideyong
     * @date 2019/11/27
     */
    @Override
    public void insertList(List<EnrollmentStuinfo> enrollmentStuinfos) {
        enrollmentStuinfoDao.insertList(enrollmentStuinfos);
    }

    /**
     * 导出数据
     *
     * @param response            响应对象
     * @param enrollmentStuinfoVo 传入参数对象
     * @return void
     * @author caideyong
     * @date 2019/12/5
     */
    @Override
    public void export(HttpServletResponse response, EnrollmentStuinfoVo enrollmentStuinfoVo) {
        List<EnrollmentStuinfo> list = listExample(enrollmentStuinfoVo);
        EasyPoiUtil.exportExcel(list, "自主招生学生信息表", "学生申报信息", EnrollmentStuinfo.class, "自主招生学生申报信息.xls", response);
    }

    public void saveHomeInfo(EnrollmentStuinfoVo enrollmentStuinfoVo) {
        //判断家庭信息是否存在
        if (StringUtils.isNotBlank(enrollmentStuinfoVo.getHomeInfo())) {
            //判断是新建还是编辑操作(status)
            if (StringUtils.isNotBlank(enrollmentStuinfoVo.getStatus())) {
                //编辑
                if (enrollmentStuinfoVo.getStatus().equals("2")) {
                    //要修改的字段
                    EnrollmentHomeinfo enrollmentHomeinfo = new EnrollmentHomeinfo();
                    enrollmentHomeinfo.setRemoveTag(DictCode.BASE_REMOVE_TAG_DELETE);
                    enrollmentHomeinfo.setUpdateTime(new Date());
                    //筛选条件
                    Example example = new Example(EnrollmentHomeinfo.class);
                    Example.Criteria criteria = example.createCriteria();
                    if (StringUtils.isNotBlank(enrollmentStuinfoVo.getId())) {
                        criteria.andEqualTo("stuinfoId", enrollmentStuinfoVo.getId());
                    }
                    criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
                    enrollmentHomeinfoDao.updateByExampleSelective(enrollmentHomeinfo, example);
                }
            }
            List<EnrollmentHomeinfoVo> enrollmentHomeinfoVoList = JSON.parseArray(enrollmentStuinfoVo.getHomeInfo(), EnrollmentHomeinfoVo.class);
            if (enrollmentHomeinfoVoList.size() > 0) {
                List<EnrollmentHomeinfo> enrollmentHomeinfoList = new ArrayList<>();
                for (EnrollmentHomeinfoVo en : enrollmentHomeinfoVoList) {
                    EnrollmentHomeinfo enrollmentHomeinfo = new EnrollmentHomeinfo();
                    enrollmentHomeinfo.setId(BaseUtil.uId());
                    enrollmentHomeinfo.setName(en.getName());
                    enrollmentHomeinfo.setPhone(en.getPhone());
                    enrollmentHomeinfo.setPosition(en.getPosition());
                    enrollmentHomeinfo.setTitle(en.getTitle());
                    enrollmentHomeinfo.setWorkPlace(en.getWorkPlace());
                    enrollmentHomeinfo.setStuinfoId(enrollmentStuinfoVo.getId());
                    enrollmentHomeinfo.setRemoveTag(BaseConstant.REMOVE_TAG_NO);
                    enrollmentHomeinfo.setCreateTime(new Date());
                    enrollmentHomeinfoList.add(enrollmentHomeinfo);
                }
                enrollmentHomeinfoService.insertList(enrollmentHomeinfoList);
            }
        }
    }

    public void saveTestInfo(EnrollmentStuinfoVo enrollmentStuinfoVo) {
        //判断考试成绩是否存在
        if (StringUtils.isNotBlank(enrollmentStuinfoVo.getTestInfo())) {
            //判断是新建还是编辑操作(status)
            if (StringUtils.isNotBlank(enrollmentStuinfoVo.getStatus())) {
                //编辑
                if (enrollmentStuinfoVo.getStatus().equals("2")) {
                    //要修改的字段
                    EnrollmentTest enrollmentTest = new EnrollmentTest();
                    enrollmentTest.setRemoveTag(DictCode.BASE_REMOVE_TAG_DELETE);
                    enrollmentTest.setUpdateTime(new Date());
                    //筛选条件
                    Example example = new Example(EnrollmentTest.class);
                    Example.Criteria criteria = example.createCriteria();
                    if (StringUtils.isNotBlank(enrollmentStuinfoVo.getId())) {
                        criteria.andEqualTo("stuinfoId", enrollmentStuinfoVo.getId());
                    }
                    criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
                    enrollmentTestDao.updateByExampleSelective(enrollmentTest, example);
                }
            }
            List<EnrollmentTestVo> enrollmentTestVoList = JSON.parseArray(enrollmentStuinfoVo.getTestInfo(), EnrollmentTestVo.class);
            if (enrollmentTestVoList.size() > 0) {
                List<EnrollmentTest> enrollmentTestList = new ArrayList<>();
                for (EnrollmentTestVo en : enrollmentTestVoList) {
                    EnrollmentTest enrollmentTest = new EnrollmentTest();
                    enrollmentTest.setId(BaseUtil.uId());
                    enrollmentTest.setName(en.getName());
                    enrollmentTest.setChinese(en.getChinese());
                    enrollmentTest.setChemistry(en.getChemistry());
                    enrollmentTest.setClassRank(en.getClassRank());
                    enrollmentTest.setEnglish(en.getEnglish());
                    enrollmentTest.setGradeRank(en.getGradeRank());
                    enrollmentTest.setHistory(en.getHistory());
                    enrollmentTest.setMath(en.getMath());
                    enrollmentTest.setPhysical(en.getPhysical());
                    enrollmentTest.setPoliticial(en.getPoliticial());
                    enrollmentTest.setStuinfoId(enrollmentStuinfoVo.getId());
                    enrollmentTest.setTotalScore(en.getTotalScore());
                    enrollmentTest.setRemoveTag(BaseConstant.REMOVE_TAG_NO);
                    enrollmentTest.setCreateTime(new Date());
                    enrollmentTestList.add(enrollmentTest);
                }
                enrollmentTestService.insertList(enrollmentTestList);
            }
        }
    }

    @Override
    public void updateUnchecked(EnrollmentStuinfoVo enrollmentStuinfoVo) {
        enrollmentStuinfoDao.updateUnchecked(enrollmentStuinfoVo);
    }

    @Override
    public List<EnrollmentStuinfo> getSchoolNum(EnrollmentStuinfoVo enrollmentStuinfoVo) {
        return enrollmentStuinfoDao.getSchoolNum(enrollmentStuinfoVo);
    }

    @Override
    public List<EnrollmentStuinfo> getSexNum(EnrollmentStuinfoVo enrollmentStuinfoVo) {
        return enrollmentStuinfoDao.getSexNum(enrollmentStuinfoVo);
    }

    @Override
    public List<EnrollmentStuinfo> getSchoolPlaceNum(EnrollmentStuinfoVo enrollmentStuinfoVo) {
        return enrollmentStuinfoDao.getSchoolPlaceNum(enrollmentStuinfoVo);
    }
}